<?php

namespace app\model;

use think\Model;
use app\model\OnJob;
use app\model\Task;
use app\model\WorkTag;

use function Qiniu\json_decode;

class Staff extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 获取在职人员
     *
     * @param string $user_id
     * @return void
     */
    public static function getonJobList($user_id = '', $time = '')
    {
        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        $where[] = ['type', '=', 1];
        // 查看全部入职人员或某合伙人下的入职人员
        if (!empty($user_id)) {
            $staff_id = self::where('boss_id', $user_id)->column('id');
            if (empty($staff_id)) {
                return ['data' => [], 'count' => 0, 'page' => 1];
            } else {
                $where[] = ['staff_id', 'in', $staff_id];
            }
        }

        // 确定入职时间
        if (!empty($time)) {
            $y = date('Y', strtotime($time));
            $m = date('m', strtotime($time));
        } else {
            $y = date('Y', time());
            $m = date('m', time());
        }

        $time = mFristAndLast($y, $m);
        $start_time = date('Y-m-d H:i:s', $time['firstday']);
        $end_time = date('Y-m-d H:i:s', $time['lastday']);

        $where[] = ['entry_time', 'between', "{$start_time}, {$end_time}"];

        $list['data'] = OnJob::where($where)
            ->field('id, name, id_num, entry_time, task_id')
            ->limit($page_count, $limit)
            ->select()->toArray();
        foreach ($list['data'] as $k => $v) {
            $task_info = Task::where('id', $v['task_id'])->find();
            $list['data'][$k]['company_name'] = $task_info['company_name'];
            $list['data'][$k]['address_sheng'] = $task_info['address_sheng'];
            $list['data'][$k]['address_shi'] = $task_info['address_shi'];
            $list['data'][$k]['address_details'] = $task_info['address_details'];
        }
        $list['count'] = OnJob::where($where)->count();
        $list['page'] = $page;
        return $list;
    }

    /**
     * 离职列表
     *
     * @param string $user_id
     * @param string $time
     * @return void
     */
    public static function leaveJobList($user_id = '', $time = '')
    {
        $where[] = ['type', '=', 3];
        // 查看全部离职人员或某合伙人下的离职人员
        if (!empty($user_id)) {
            $staff_id = self::where('boss_id', $user_id)->column('id');
            if (empty($staff_id)) {
                return ['data' => [], 'count' => 0, 'page' => 1];
            } else {
                $where[] = ['staff_id', 'in', $staff_id];
            }
        }

        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        // 确定离职时间
        if (!empty($time)) {
            $y = date('Y', strtotime($time));
            $m = date('m', strtotime($time));
        } else {
            $y = date('Y', time());
            $m = date('m', time());
        }

        $time = mFristAndLast($y, $m);
        $start_time = date('Y-m-d H:i:s', $time['firstday']);
        $end_time = date('Y-m-d H:i:s', $time['lastday']);

        $where[] = ['leave_time', 'between', "{$start_time}, {$end_time}"];

        $list['data'] = OnJob::where($where)
            ->field('id, name, id_num, phone, leave_time, cause')
            ->limit($page_count, $limit)
            ->select()
            ->toArray();
        $list['count'] = OnJob::where($where)->count();
        $list['page'] =$page;
        return $list;
    }

    /**
     * 待入职人员列表
     *
     * @return void
     */
    public static function waitOnJobList($user_id = '')
    {
        $staff_id = OnJob::where('type', 1)->column('staff_id');

        $where = [];
        if (!empty($user_id)) {
            $where[] = ['boss_id', '=', $user_id];
        }

        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        $list['data'] = self::field('id, name, sex, age, id_num, phone, create_time, expect_work, before_work, compensation, is_ill')
            ->where($where)
            ->whereNotIn('id', $staff_id)
            ->withAttr('expect_work', function($value, $data) {
                return json_decode($value);
            })
            ->limit($page_count, $limit)
            ->select()
            ->toArray();
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['expect_work_name'] = WorkTag::whereIn('id', $v['expect_work'])->column('tag');
        }
        $list['count'] = self::where($where)
            ->whereNotIn('id', $staff_id)
            ->count();
        $list['page'] =$page;
        return $list;
    }

}